﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KVB.Common;
using Customer_Profile.eCard.DBDao;

namespace Customer_Profile.eCard.Agent
{
    public partial class SettleAgentAdd : System.Web.UI.Page
    {
        string SettleAgent_ID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["settle_agent_id"] != null)
            {
                SettleAgent_ID = Request["settle_agent_id"].ToString();
            }
            if (!Page.IsPostBack)
            {
                //Comm.BindDll(ref dropPeriodType, "TermUnit", false, "");
                if (!SettleAgent_ID.Equals(""))
                {
                    bindPage();
                    btnSave.Enabled = false;
                }
                else
                {
                    btnEdit.Enabled = false;
                }
            }
        }

        private void bindPage()
        {
            try
            {
                DataSet ds = SaleAgentDao.getSettleAgentbyID(int.Parse(SettleAgent_ID));
                txtsale_agent_name.Value = ds.Tables[0].Rows[0]["settle_agent_name"].ToString();
                txtlinkman.Value = ds.Tables[0].Rows[0]["linkman"].ToString();
                txtphone.Value = ds.Tables[0].Rows[0]["phone"].ToString();
                txtemail.Value = ds.Tables[0].Rows[0]["email"].ToString();
                txtaddress.Value = ds.Tables[0].Rows[0]["address"].ToString();
                txtremark.Value = ds.Tables[0].Rows[0]["remark"].ToString();
                dropIf_used.Items.FindByValue(ds.Tables[0].Rows[0]["in_use"].ToString().Trim()).Selected = true;
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("SettleAgentAdd_bindPage", ex.ToString());
                Response.Write(ex.ToString());
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string settle_agent_name = txtsale_agent_name.Value.Trim();
                string linkman = txtlinkman.Value.Trim();
                string phone = txtphone.Value.Trim();
                string email = txtemail.Value.Trim();
                string address = txtaddress.Value.Trim();
                //string saleclass = txtclass.Value.Trim();
                string remark = txtremark.Value.Trim();
                string in_use = dropIf_used.SelectedItem.Value;

                bool bok = SaleAgentDao.SettleAgentAdd(settle_agent_name, linkman, phone, email, address, remark, in_use); ;
                if (bok)
                    KVB.Common.JavaScript.ViewMsgGoUrl("SettleAgentList.aspx", "添加成功");
                else
                    Response.Write("<script language=javascript>alert('添加失败');</script>");
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("SettleAgentAdd.btnSave_Click", ex.ToString());
                KVB.Common.JavaScript.ViewAlertMsg(ex.ToString());
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("SettleAgentList.aspx");
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                string settle_agent_name = txtsale_agent_name.Value.Trim();
                string linkman = txtlinkman.Value.Trim();
                string phone = txtphone.Value.Trim();
                string email = txtemail.Value.Trim();
                string address = txtaddress.Value.Trim();                
                string remark = txtremark.Value.Trim();
                string in_use = dropIf_used.SelectedItem.Value;

                bool bok = SaleAgentDao.SettleAgentUpdate(int.Parse(SettleAgent_ID), settle_agent_name, linkman, phone, email, address, remark, in_use);

                if (bok)
                    Response.Write("<script language=javascript>alert('修改成功');</script>");
                else
                    Response.Write("<script language=javascript>alert('修改失败');</script>");
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("addcflow.btnEdit_Click", ex.ToString());
                KVB.Common.JavaScript.ViewAlertMsg(ex.ToString());
            }
        }
    }
}